package com.order.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.order.manager.Model.*;
import com.order.manager.Model.other.MenuCount;
import com.order.manager.Model.other.OrderForSender;
import com.order.manager.mapper.OrderInfoMapper;
import com.order.manager.service.OrderInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 24862
 * @description 针对表【order_info】的数据库操作Service实现
 * @createDate 2024-04-08 16:43:01
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Resource
    OrderInfoMapper orderInfoMapper;

    @Resource
    MenuInfoServiceImpl menuInfoService;

    @Resource
    ShopInfoServiceImpl shopInfoService;

    @Resource
    SenderInfoServiceImpl senderInfoService;

    @Resource
    UserInfoServiceImpl userInfoService;

    @Resource
    UserAddressServiceImpl userAddressService;

    @Resource
    OrderMenuInfoServiceImpl orderMenuInfoService;

    @Resource
    OrderSendInfoServiceImpl orderSendInfoService;

    @Override
    public PageInfo<OrderForSender> querySenderFindList(int page, int item) {
        PageMethod.startPage(page, item);
        List<OrderForSender> orderForSenderList = new ArrayList<>();
        List<OrderInfo> orderInfoList = orderInfoMapper.queryIsApplyList();//订单为已支付
        for (OrderInfo orderInfo : orderInfoList) {
            if (orderSendInfoService.count(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId()).eq("state", 0)) != 0 || orderSendInfoService.count(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId()).eq("state", 1)) != 0) {
                continue;
            }
            OrderForSender order = new OrderForSender();
            order.setInputTime(orderInfo.getInputTime());


            if (userAddressService.count(new QueryWrapper<UserAddress>().eq("address_id", orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId())) == 0 || userAddressService.getById(orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId()).getAddress() == null) {
                order.setAddress("");
            } else {
                order.setAddress(userAddressService.getById(orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId()).getAddress());
            }
            List<OrderMenuInfo> orderMenuInfos = orderMenuInfoService.list(new QueryWrapper<OrderMenuInfo>().eq("order_id", orderInfo.getOrderId()));
            List<MenuCount> counts = new ArrayList<>();
            for (OrderMenuInfo orderMenuInfo : orderMenuInfos) {
                MenuCount menuCount = new MenuCount();
                menuCount.setCount(orderMenuInfo.getCount());
                menuCount.setMenuId(orderMenuInfo.getMenuId());
                menuCount.setMenuName(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuName());
                menuCount.setMenuPicture(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPicture());
                menuCount.setMenuPrice(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPrice());
                counts.add(menuCount);
            }
            order.setDemo(orderInfo.getDemo());
            order.setOrderId(orderInfo.getOrderId());
            order.setMenuName(counts);
            order.setShopName(shopInfoService.getById(orderInfo.getShopId()).getShopName());
            order.setState("待接单");
            order.setUserId(orderInfo.getUserId());
            order.setUsername(userInfoService.getById(orderInfo.getUserId()).getUsername());
            if (userInfoService.getById(orderInfo.getUserId()).getPhone() == null) {
                order.setUserPhone("");

            } else {
                order.setUserPhone(userInfoService.getById(orderInfo.getUserId()).getPhone());
            }
            orderForSenderList.add(order);
        }
        return new PageInfo<>(orderForSenderList);
    }

    @Override
    public PageInfo<OrderForSender> queryByMyself(int page, int item, String senderId) {
        List<OrderSendInfo> orderSendInfos = orderSendInfoService.list(new QueryWrapper<OrderSendInfo>().eq("sender_id", senderId));
        List<OrderForSender> orderForSenders = new ArrayList<>();
        for (OrderSendInfo orderSendInfo : orderSendInfos) {
            OrderForSender orderForSender = new OrderForSender();
            OrderInfo order = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", orderSendInfo.getOrderId()));
            if (order == null) continue;
            orderForSender.setOrderId(order.getOrderId());
            orderForSender.setState(orderSendInfo.getState() == 0 ? "正在配送" : orderSendInfo.getState() == 1 ? "已送达" : orderSendInfo.getState() == -1 ? "骑手已退单，待其他骑手接单" : "未知");
            if (userAddressService.count(new QueryWrapper<UserAddress>().eq("address_id", orderInfoMapper.selectById(orderSendInfo.getOrderId()).getAddressId())) == 0 || userAddressService.getById(orderInfoMapper.selectById(orderSendInfo.getOrderId()).getAddressId()).getAddress() == null) {
                orderForSender.setAddress("");
            } else {
                orderForSender.setAddress(userAddressService.getById(orderInfoMapper.selectById(orderSendInfo.getOrderId()).getAddressId()).getAddress());
            }
            List<OrderMenuInfo> orderMenuInfos = orderMenuInfoService.list(new QueryWrapper<OrderMenuInfo>().eq("order_id", orderSendInfo.getOrderId()));
            List<MenuCount> menuCounts = new ArrayList<>();
            for (OrderMenuInfo orderMenuInfo : orderMenuInfos) {
                MenuCount menuCount = new MenuCount();
                menuCount.setMenuName(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuName());
                menuCount.setMenuPrice(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPrice());
                menuCount.setCount(orderMenuInfo.getCount());
                menuCount.setMenuPicture(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPicture());
                menuCount.setMenuId(orderMenuInfo.getMenuId());
                menuCounts.add(menuCount);
            }
            orderForSender.setMenuName(menuCounts);
            orderForSender.setShopName(shopInfoService.getById(order.getShopId()).getShopName());
            orderForSender.setInputTime(order.getInputTime());
            orderForSender.setUserId(order.getUserId());
            orderForSender.setUsername(userInfoService.getById(order.getUserId()).getName());
            orderForSender.setSenderName(userInfoService.getById(senderInfoService.getById(senderId).getUserId()).getName());
            orderForSender.setSenderId(orderSendInfo.getSenderId());
            if (userInfoService.count(new QueryWrapper<UserInfo>().eq("user_id", order.getUserId())) == 0 || userInfoService.getById(order.getUserId()).getPhone() == null) {
                orderForSender.setUserPhone("未知");
            } else {
                orderForSender.setUserPhone(userInfoService.getById(order.getUserId()).getPhone());
            }
            if (userInfoService.count(new QueryWrapper<UserInfo>().eq("user_id", orderSendInfo.getSenderId())) == 0 || userInfoService.getById(orderSendInfo.getSenderId()).getPhone() == null) {
                orderForSender.setSenderPhone("未知");
            } else {
                orderForSender.setSenderPhone(userInfoService.getById(orderSendInfo.getSenderId()).getPhone());
            }
            orderForSender.setDemo(order.getDemo() == null ? "" : order.getDemo());
            orderForSenders.add(orderForSender);
        }
        return new PageInfo<>(orderForSenders);
    }

    @Override
    public PageInfo<OrderForSender> queryByShop(int page, int item, String shopId) {
        PageMethod.startPage(page, item);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(new QueryWrapper<OrderInfo>().eq("shop_id", shopId));
        List<OrderForSender> orderForSenders = new ArrayList<>();
        for (OrderInfo orderInfo : orderInfoList) {
            if (orderInfo.getApplyState() == 0) {
                continue;
            }
            OrderForSender order = new OrderForSender();
            order.setInputTime(orderInfo.getInputTime());
            if (userAddressService.count(new QueryWrapper<UserAddress>().eq("address_id", orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId())) == 0 || userAddressService.getById(orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId()).getAddress() == null) {
                order.setAddress("");
            } else {
                order.setAddress(userAddressService.getById(orderInfoMapper.selectById(orderInfo.getOrderId()).getAddressId()).getAddress());
            }
            List<OrderMenuInfo> orderMenuInfos = orderMenuInfoService.list(new QueryWrapper<OrderMenuInfo>().eq("order_id", orderInfo.getOrderId()));
            List<MenuCount> counts = new ArrayList<>();
            for (OrderMenuInfo orderMenuInfo : orderMenuInfos) {
                MenuCount menuCount = new MenuCount();
                menuCount.setCount(orderMenuInfo.getCount());
                menuCount.setMenuId(orderMenuInfo.getMenuId());
                menuCount.setMenuName(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuName());
                menuCount.setMenuPicture(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPicture());
                menuCount.setMenuPrice(menuInfoService.getById(orderMenuInfo.getMenuId()).getMenuPrice());
                counts.add(menuCount);
            }
            order.setMoney(orderInfo.getMoney().toString());
            if (orderInfo.getType() == 1) {
                order.setType("堂食");
            } else {
                order.setType("外卖");
            }
            if (orderInfo.getApplyState() == 1) {
                order.setApplyState("已支付");
            } else {
                order.setApplyState("已退款");
            }
            order.setDemo(orderInfo.getDemo() == null ? "" : orderInfo.getDemo());
            order.setOrderId(orderInfo.getOrderId());
            order.setMenuName(counts);
            order.setShopName(shopInfoService.getById(orderInfo.getShopId()).getShopName());
            order.setUserId(orderInfo.getUserId());
            if (userInfoService.count(new QueryWrapper<UserInfo>().eq("user_id", orderInfo.getUserId())) == 0 || userInfoService.getById(orderInfo.getUserId()).getPhone() == null) {
                order.setUserPhone("");
            }
            order.setUsername(userInfoService.getById(orderInfo.getUserId()).getUsername());
            //是否配送员接单
            if (orderInfo.getType() == 2) {
                order.setSenderName("");
                order.setSenderPhone("");
                order.setState("待骑手接单");
                order.setSenderId("");
                if (orderSendInfoService.count(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId())) != 0) {
                    SenderInfo senderInfo = senderInfoService.getOne(new QueryWrapper<SenderInfo>().eq("sender_id", orderSendInfoService.getOne(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId())).getSenderId()));
                    order.setSenderId(senderInfo.getSenderId());
                    order.setSenderName(userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id", senderInfo.getUserId())).getName());
                    if (userInfoService.count(new QueryWrapper<UserInfo>().eq("user_id", senderInfo.getUserId())) != 0 ||//获取配送员信息
                            userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id", senderInfo.getUserId())).getPhone() != null) {
                        order.setSenderPhone(userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id", senderInfo.getUserId())).getPhone());
                    }
                }
                if (orderSendInfoService.count(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId())) != 0) {
                    order.setState(orderSendInfoService.getOne(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId())).getState() == 1 ? "已送达" : orderSendInfoService.getOne(new QueryWrapper<OrderSendInfo>().eq("order_id", orderInfo.getOrderId())).getState() == 0 ? "正在配送中" : "未知");
                }
            }
            order.setUsername(userInfoService.getById(orderInfo.getUserId()).getName());
            if (userInfoService.getById(orderInfo.getUserId()).getPhone() == null) {
                order.setUserPhone("未知");
            } else {
                order.setUserPhone(userInfoService.getById(orderInfo.getUserId()).getPhone());
            }
            orderForSenders.add(order);
        }
        return new PageInfo<>(orderForSenders);
    }
}




